//
// Created by squbirreland on 2021/11/19.
//
#include "result.h"
#include "stdlib.h"

char *err_message(enum ErrorCode err) {
    if (err == Unknown) {
        return "unknown err";
    } else {
        return NULL;
    }
}

struct Result *result_success(void const *p) {
    struct Result *r = malloc(sizeof(struct Result));
    r->success = 1;
    r->p = p;
    return r;
}

struct Result *result_failed(enum ErrorCode err) {
    struct Result *r = malloc(sizeof(struct Result));
    r->success = 0;
    r->err = err;
    return r;
}

void const *result_panic(struct Result *result) {
    if (result->success) {
        void const *p = result->p;
        free(result);
        return p;
    } else {
        exit(result->err);
    }
}